/*
 * Created on 2007-10-15
 *
 */
package jacky.lanlan.song.extension.strecks.controller;

import jacky.lanlan.song.extension.strecks.action.FormMappingAction;

import org.apache.struts.action.ActionForm;
import org.strecks.context.ActionContext;
import org.strecks.controller.annotation.ActionInterface;
import org.strecks.form.controller.BindingForm;

/**
 * 配套FormMappingAction的Controller
 * 
 * @author Jacky.Song
 */
@ActionInterface(name = FormMappingAction.class)
public class FormMappingController extends MappingBackedController {

	@Override
	protected void beforeMappingNavigation(Object actionBean, ActionContext context) {
		// 检测Form是否有错，并且设置绑定标记(和BasicFormController的executeAction()方法几乎相同)
		FormMappingAction action = (FormMappingAction) actionBean;

		ActionForm form = context.getForm();

		boolean hasErrors = hasErrors(context);
		action.setInputError(hasErrors);

		if (form instanceof BindingForm && !hasErrors) {
			BindingForm bindingForm = (BindingForm) form;
			bindingForm.setBindOutwards(true);
		}

	}

	@Override
	protected void afterMappingNavigation(Object actionBean, ActionContext context) {
		ActionForm form = context.getForm();
		if (form instanceof BindingForm) {
			BindingForm validBindingForm = (BindingForm) form;// form是一个DelegatingForm，
																												// 内含真正的Struts生成的ActionForm
			
			if (validBindingForm.getBindOutwards()) {
				/*
				 * Out绑定：
				 * 扫描所有带Bind标注的form getter方法，通过expression把form里Domain Object的属性值，
				 * 注入form的对应属性(使用convertor进行转换)。
				 * 
				 * 用于渲染form
				 */
				validBindingForm.bindOutwards(actionBean);// 注意，这个actionBean在BindSimple时并没有使用
			}
		}
	}

}
